package integral

import (
	"context"
	"fmt"
	"github.com/shopspring/decimal"
	"private-domain-overseas-service/pkg/common"
	"private-domain-overseas-service/pkg/constant"
	"private-domain-overseas-service/pkg/status/bizsystemconfig"
	"private-domain-overseas-service/pkg/xerr"

	"private-domain-overseas-service/app/business/api/internal/svc"
	"private-domain-overseas-service/app/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type IntegralRewardLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取新用户奖励积分
func NewIntegralRewardLogic(ctx context.Context, svcCtx *svc.ServiceContext) *IntegralRewardLogic {
	return &IntegralRewardLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *IntegralRewardLogic) IntegralReward() (resp *types.IntegralRewardResponse, err error) {
	//systemConfigDao := system.NewSystemConfigDao(l.ctx, l.svcCtx)
	//定义换算单位
	baiDecimal := decimal.NewFromInt(100)

	//查询配置表
	configValue, err := common.GetSystemConfig(constant.HzAdminSystemConfigKey, bizsystemconfig.NewUserIntegralReaward, l.svcCtx.BizRedis, l.svcCtx.Orm)
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerDataNotFoundError, fmt.Sprintf("查询新用户奖励配置信息失败：%v", err.Error()))
	}

	//积分换算
	integralFen, _ := decimal.NewFromString(configValue)

	//返回数据
	resp = &types.IntegralRewardResponse{Integral: integralFen.Div(baiDecimal).StringFixed(2)}
	return resp, nil
}
